package com.health.jobs;

import com.health.constant.RedisConstant;
import com.health.utils.FileUtils;
import com.health.utils.PropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @Classname ClearImgJob
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/16 16:41
 * @Author RainGrd
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;

    private static final PropertiesUtils propertiesUtils = PropertiesUtils.getInstance();


    public void clearImg() {
        //根据Redis中保存的两个set集合进行差值计算，获得垃圾图片名称集合
        Set<String> set =
                jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,
                        RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null) {
            for (String picName : set) {
                //删除本地存储的图片
                FileUtils.deleteFile(picName);
                //从Redis集合中删除图片名称
                jedisPool.getResource().
                        srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
                System.out.println("自定义任务执行,清理垃圾图片");
            }
        }
    }
}
